package com.sdb.patients.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sdb.common.annotation.Log;
import com.sdb.common.core.controller.BaseController;
import com.sdb.common.core.domain.AjaxResult;
import com.sdb.common.enums.BusinessType;
import com.sdb.patients.domain.TdPatients;
import com.sdb.patients.service.ITdPatientsService;
import com.sdb.common.utils.poi.ExcelUtil;
import com.sdb.common.core.page.TableDataInfo;

/**
 * 病人信息Controller
 * 
 * @author yyh
 * @date 2024-12-26
 */
@RestController
@RequestMapping("/patients/patientlist")
public class TdPatientsController extends BaseController
{
    @Autowired
    private ITdPatientsService tdPatientsService;

    /**
     * 查询病人信息列表
     */
    @PreAuthorize("@ss.hasPermi('patients:patientlist:index')")
    @GetMapping("/list")
    public TableDataInfo list(TdPatients tdPatients)
    {
        startPage();
        List<TdPatients> list = tdPatientsService.selectTdPatientsList(tdPatients);
        return getDataTable(list);
    }

    /**
     * 导出病人信息列表
     */
    @PreAuthorize("@ss.hasPermi('patients:patientlist:index')")
    @Log(title = "病人信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdPatients tdPatients)
    {
        List<TdPatients> list = tdPatientsService.selectTdPatientsList(tdPatients);
        ExcelUtil<TdPatients> util = new ExcelUtil<TdPatients>(TdPatients.class);
        util.exportExcel(response, list, "病人信息数据");
    }

    /**
     * 获取病人信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('patients:patientlist:index')")
    @GetMapping(value = "/{patientsId}")
    public AjaxResult getInfo(@PathVariable("patientsId") String patientsId)
    {
        return success(tdPatientsService.selectTdPatientsByPatientsId(patientsId));
    }

    /**
     * 新增病人信息
     */
    @PreAuthorize("@ss.hasPermi('patients:patientlist:patientadd')")
    @Log(title = "病人信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdPatients tdPatients)
    {
        int result = tdPatientsService.insertTdPatients(tdPatients);
        if (result == 0) {
            return AjaxResult.error("该患者已注册");
        }
        return toAjax(result);
    }

    /**
     * 修改病人信息
     */
    @PreAuthorize("@ss.hasPermi('patients:patientlist:patientadd')")
    @Log(title = "病人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdPatients tdPatients)
    {
        return toAjax(tdPatientsService.updateTdPatients(tdPatients));
    }

    /**
     * 删除病人信息
     */
    @PreAuthorize("@ss.hasPermi('patients:patientlist:patientadd')")
    @Log(title = "病人信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patientsIds}")
    public AjaxResult remove(@PathVariable String[] patientsIds)
    {
        return toAjax(tdPatientsService.deleteTdPatientsByPatientsIds(patientsIds));
    }

    /**
     * 根据就诊卡号查询病人信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping(value = "/getbycard/{cardId}")
    public TdPatients getByCard(@PathVariable("cardId") String cardId) {
        return tdPatientsService.selectTdPatientsByCard(cardId);
    }

}
